package cn.buk.tms.eotms.util;

import cn.buk.common.flight.dto.FlightInfoDto;
import cn.buk.common.util.DateUtil;
import cn.buk.tms.common.dto.base.BaseOrderDto;
import cn.buk.tms.common.dto.base.BasePassengerDto;
import cn.buk.tms.common.dto.base.BasePriceDto;
import cn.buk.tms.eotms.entity.FlightOrder;
import cn.buk.tms.eotms.entity.FlightOrderFlight;
import cn.buk.tms.eotms.entity.FlightOrderPassenger;
import cn.buk.tms.eotms.entity.OrderApprovalInfo;
import cn.buk.tms.eotms.request.FlightOrderRequestDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yfdai
 */
public class OrderUtil {

    public static void copyFlightOrderPrice(FlightOrderRequestDto dto, FlightOrder info) {
        dto.calcCost(); // 需要计算成人和儿童的数量

        for(BasePriceDto priceDto: dto.getPrices()) {
            if (priceDto.getPriceType() == 0) {
                BasePriceDto o = priceDto;
                // 成人价格
                info.setPrice(o.getPrice());
                info.setParvalue(o.getParValue());
                info.setTax(o.getTax());
                info.setInsurance(o.getInsurance());
                info.setServiceCharge(o.getServiceCharge());
                info.setDiscount(o.getDiscount());
                info.setAmount(o.getAmount());
            } else if (priceDto.getPriceType() == 1) {
                // 儿童价格
                BasePriceDto o = priceDto;
                info.setChdPrice(o.getPrice());
                info.setChdParvalue(o.getParValue());
                info.setChdTax(o.getTax());
                info.setChdInsurance(o.getInsurance());
                info.setChdServiceCharge(o.getServiceCharge());
                info.setChdDiscount(o.getDiscount());
                info.setChdAmount(o.getAmount());

            } else if (priceDto.getPriceType() == 2) {
                // 婴儿价格
                BasePriceDto o = priceDto;
                info.setInfPrice(o.getPrice());
                info.setInfParvalue(o.getParValue());
                info.setInfTax(o.getTax());
                info.setInfInsurance(o.getInsurance());
                info.setInfServiceCharge(o.getServiceCharge());
                info.setInfDiscount(o.getDiscount());
                info.setInfAmount(o.getAmount());
            }
        }

        info.setTotalAmount(dto.getTotalAmount());
    }

    public static void copyFlightOrderFlights(FlightOrderRequestDto dto, FlightOrder info) {
        List<FlightOrderFlight> flts = info.getFlights();

        info.setRouteType(dto.getRouteType());

        int segOrd = 0;
        String airportName;
        for (FlightInfoDto flt : dto.getFlights()) {
            segOrd++;

            FlightOrderFlight detail = new FlightOrderFlight();

            BeanUtils.copyProperties(flt, detail.getFlight());
            detail.getFlight().setDepartureDate(DateUtil.formatDate(flt.getDdate(), "yyyy-MM-dd"));
            detail.getFlight().setArrivalDate(DateUtil.formatDate(flt.getAdate(), "yyyy-MM-dd"));

            detail.setEnterpriseId(info.getEnterpriseId());
            detail.setSegOrder(segOrd);
            detail.setOrder(info);

            //退改条件
            detail.setChangeRule(flt.getChangeRule());
            detail.setRefundRule(flt.getRefundRule());

            //物理舱位
            detail.getFlight().setCabinClass(flt.getCabinClass());

            flts.add(detail);
        }

        int segCount = flts.size();
        info.setSegCount(segCount);
        if (info.getRouteType() == 0) {
            //未传入时，通过航段就行计算
            //TODO 国际航班（转机）时，会出错
            if (segCount == 1) {
                info.setRouteType(1);
            } else if (segCount == 2) {
                if (flts.get(0).getFlight().getArrivalAirport().equals(flts.get(1).getFlight().getDepartureAirport())) {
                    info.setRouteType(2);
                } else {
                    info.setRouteType(3);
                }
            } else {
                info.setRouteType(3);
            }
        }
    }

    public static void copyFlightOrderPassengers(FlightOrderRequestDto dto, FlightOrder info) {
        List<FlightOrderPassenger> psgs = info.getPassengers();

        for (BasePassengerDto psgDto : dto.getPassengers()) {
            if (!psgDto.isSelected()) {
              continue;
            }

            FlightOrderPassenger detail = new FlightOrderPassenger();

            BeanUtils.copyProperties(psgDto, detail, "name", "firstName", "lastName");

            detail.setEnterpriseId(info.getEnterpriseId());
            detail.setOrder(info);

            if (psgDto.getName() != null && psgDto.getName().trim().length() > 0) {
                //中文名字模式
                detail.setName(psgDto.getName());
            } else {
                //英文名字模式
                detail.setFirstName(psgDto.getFirstName().trim().toUpperCase());
                detail.setLastName(psgDto.getLastName().trim().toUpperCase());
                detail.setName(detail.getFirstName() + "/" + detail.getLastName());
            }
            detail.setIdNo(psgDto.getIdNo());
            detail.setPsgType(psgDto.getPsgType());

            psgs.add(detail);
        }

        info.setPsgCount(psgs.size());
    }

    public static void copyApprovalInfo(OrderApprovalInfo src, BaseOrderDto dest) {
        if (src == null || dest == null) {
            return;
        }

        dest.setSpecifiedApprover(src.getSpecifiedApprover());
        dest.setApprovalStatus(src.getApprovalStatus());
        dest.setApprovalDenyReason(src.getApprovalDenyReason());
        dest.setViolationStatus(src.getViolationStatus());
        dest.setViolationReason(src.getViolationReason());
    }

}
